﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Documents;

namespace Alimentador.RightGroup.Patient
{
    /// <summary>
    /// Interaction logic for LoadPatientListPopUp.xaml
    /// </summary>
    public partial class LoadLocalFileListPopUp : Window
    {
        private Action<string> m_printFnc;
        public LoadLocalFileListPopUp(string path, string FILE_EXTENSION, Action<string> printFnc)
        {
            InitializeComponent();

            m_printFnc = printFnc;
            if (m_printFnc != null)
            {
                PrintBtn.Visibility = Visibility.Visible;
            }
            else
            {
                PrintBtn.Visibility = Visibility.Collapsed;
            }

            IEnumerable<string> ItemListPath = Directory.EnumerateFiles(path);

            List<string> ItemList_str = new List<string>();
            foreach (string str in ItemListPath)
            {
                string FileName = str.Replace(path, "");
                FileName = FileName.Replace(FILE_EXTENSION, "");
                ItemList_str.Add(FileName);
            }

            PatientList.ItemsSource = ItemList_str;
        }

        public void SetInstructions(string title)
        {
            if(Instructions_tb != null)
            {
                Instructions_tb.Text = title;
            }
        }

        internal string getSelectedItem()
        {
            if (PatientList.SelectedIndex != -1)
            {
                return (string)PatientList.SelectedItem;
            }
            return "";
        }

        private void OpenBtn_Click(object sender, RoutedEventArgs e)
        {
            if (PatientList.SelectedIndex != -1)
            {
                Close();
            }
            else 
            {
                MessageBox.Show("Escolha primeiro qual o paciente que deseja abrir.", "Atenção", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }

        private void CancelBtn_Click(object sender, RoutedEventArgs e)
        {
            PatientList.SelectedIndex = -1;
            Close();
        }

        private void HandleDoubleClick(object sender, RoutedEventArgs e)
        {
            if(PatientList.SelectedIndex != -1)
                Close();
        }

        private void PrintBtn_Click(object sender, RoutedEventArgs e)
        {
            if (m_printFnc != null)
            {
                if (PatientList.SelectedItem != null)
                {
                    m_printFnc(PatientList.SelectedItem.ToString());
                }
            }
        }
    }
}